package edu.washington.cs.workflow.analysis.guistructure.uievents;

import javax.swing.JTabbedPane;

import org.uispec4j.Panel;
import org.uispec4j.TabGroup;
import org.uispec4j.UIComponent;
import org.uispec4j.finder.StringMatcher;

import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent.UIAction;
import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent.WINDOW;
import edu.washington.cs.workflow.util.Utils;

public class TabGroupEvent extends AbstractUIEvent  {

	private final TabGroup tabGroup;
	private final UIAction action;
	private final int selectedIndex;
	
	public TabGroupEvent(TabGroup tabGroup, Panel panel,
			UIAction action, int selectedIndex) {
		Utils.checkNotNull(tabGroup);
		Utils.checkNotNull(panel);
		Utils.checkNotNull(action);
		Utils.checkTrue(action.equals(UIAction.Click), "Illegal action: " + action);
		Utils.checkTrue(selectedIndex >= 0 && selectedIndex < tabGroup.getAwtComponent().getTabCount());
		this.tabGroup = tabGroup;
		this.setCurrentPanel(panel);
		this.action = action;
		this.selectedIndex = selectedIndex;
	}
	
	@Override
	public UIComponent getUIComponent() {
		return this.tabGroup;
	}

	@Override
	public UIAction getAction() {
		return this.action;
	}
	
	public int getSelectedIndex() {
		return this.selectedIndex;
	}

	@Override
	public String toString() {
		String tablabel = this.getTabLabel(selectedIndex);
		return this.action + ", TabGroup, index: " + this.selectedIndex + ", with label: " + tablabel;
	}
	
	@Override
	public String getDescription() {
		return this.action + ", TabGroup, index: " + this.tabGroup.getName()
		    + ",  " + this.tabGroup.getLabel() + this.selectedIndex ;
	}
	
	@Override
	protected WINDOW probeWindowInfoInternal() {
		return WINDOW.NONE;
	}
	
	@Override
	public boolean canFireEvent() {
		//check if it is legal
		if(selectedIndex >= tabGroup.getAwtComponent().getTabCount()) {
			return false;
		}
		return this.tabGroup.isEnabled().isTrue();
	}

	@Override
	//FIXME  should add the new panel as a non-modal?
	protected UIEventOutcome fireEventInternal() {
		
		UIEventOutcome outcome = new UIEventOutcome();
		Throwable error = null;
		try {
			String tabLabel = this.getTabLabel(selectedIndex);
			this.tabGroup.selectTab(tabLabel);
		}  catch (Throwable e) {
			error = e;
		}
		
		if(error != null) {
		    outcome.setError(error);
		}
		outcome.currentPanelClosed(super.isCurrentPanelClosed());
		return outcome;
	}

	private String getTabLabel(int index) {
		Utils.checkTrue(index < this.tabGroup.getAwtComponent().getTabCount(), 
				"The index: " + index + ", the tab group count: "
				+ this.tabGroup.getAwtComponent().getTabCount());
		JTabbedPane jTabbedPane = this.tabGroup.getAwtComponent();
	    for (int i = 0; i < jTabbedPane.getTabCount(); i++) {
	      String title = jTabbedPane.getTitleAt(i);
	      if(i == index) {
	    	  return title;
	      }
	    }
	    throw new Error("illegal index: " + index);
	}
}